<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-suffix="：" label-width="120px">
      <el-form-item label="教体局" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入教体局名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="社会信用代码" prop="creditCode">
        <el-input v-model="queryParams.creditCode" placeholder="请输入社会信用代码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['regulatory:bureau:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['regulatory:bureau:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bureauList" border style="width: 1000px;">
      <el-table-column label="" align="center" prop="id" width="60"/>
      <el-table-column label="教体局名称" align="left" prop="name"/>
      <el-table-column label="社会信用代码" align="center" prop="creditCode" width="200"/>
      <el-table-column label="监管区域" align="center" prop="areaName" width="200"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-user" @click="handleShowAdmin(scope.row)">管理员</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['regulatory:bureau:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['regulatory:bureau:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改教体局信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-suffix="：" label-width="120px">
        <el-form-item label="教体局" prop="name">
          <el-input v-model="form.name" placeholder="请输入教体局名称" maxlength="50"/>
        </el-form-item>
        <el-form-item label="社会信用代码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入社会信用代码" maxlength="30" />
        </el-form-item>
        <el-form-item label="监管区域" prop="area">
          <el-select v-model="form.areaId" placeholder="请选择监管区域" clearable>
            <el-option v-for="area in areaList" :key="area.id" :label="area.name" :value="area.id"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <RegulatoryAdmin ref="regulatoryAdminRef" user-type="01"/>
  </div>
</template>

<script>
import {listBureau, getBureau, delBureau, addBureau, updateBureau} from "@/api/regulatory/bureau";
import RegulatoryAdmin from '@/views/regulatory/components/regulatory_admin'
import {listArea} from "@/api/basic/area";

export default {
  name: "Bureau",
  components: {RegulatoryAdmin},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教体局信息表格数据
      bureauList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      areaList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        creditCode: null,
      },
      // 表单参数
      form: {
        name: undefined,
        creditCode: undefined,
        areaId: undefined,
      },
      // 表单校验
      rules: {
        name: [{required: true, message: '请输入教体局名称', trigger: 'blur'}],
      }
    };
  },
  created() {
    this.getList();

    this.getAllArea()
  },
  methods: {
    /** 查询教体局信息列表 */
    getList() {
      this.loading = true;
      listBureau(this.queryParams).then(response => {
        this.bureauList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAllArea() {
      listArea({pageSize: 999999}).then(response => {
        this.areaList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        creditCode: null,
        createTime: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教体局信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBureau(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教体局信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBureau(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBureau(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除教体局信息编号为"' + ids + '"的数据项？').then(function() {
        return delBureau(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('regulatory/bureau/export', {
        ...this.queryParams
      }, `bureau_${new Date().getTime()}.xlsx`)
    },
    handleShowAdmin(row) {
      this.$refs.regulatoryAdminRef.openAdmin(row.id)
    }
  }
};
</script>
